package ren.steve.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import ren.steve.annotation.Log;
import ren.steve.common.result.HttpStatus;
import ren.steve.common.result.Result;
import ren.steve.enums.BusinessType;
import ren.steve.model.system.bean.SystemApp;
import ren.steve.model.system.vo.SystemAppQueryVo;
import ren.steve.system.service.SystemAppService;

import java.util.List;

/**
 * @Author: LiuGuoJia
 * @Description:
 * @Date: Created in 2024/1/18 20:03
 */

@Api(tags = "APIs-微前端子服务管理")
@CrossOrigin
@RestController
@RequestMapping("/api/system/system-app")
public class SystemAppController {

  @Autowired
  private SystemAppService systemAppService;

  @ApiOperation("API-查询所有")
  @PreAuthorize("hasAuthority('/api/system/system-app/list')")
  @Log(title = "根据ID查询单条数据", businessType = BusinessType.GET)
  @GetMapping("list")
  public Result findList (String user) {
    List<SystemApp> apps = systemAppService.list();
    return Result.ok(apps, HttpStatus.OK);
  }


  @ApiOperation("API-根据ID查询单条数据")
  @Log(title = "根据ID查询单条数据", businessType = BusinessType.GET)
  @PreAuthorize("hasAuthority('/api/system/system-app/detail')")
  @GetMapping("detail")
  public Result findById (String id) {
    SystemApp app = systemAppService.getById(id);
    return Result.ok(app, HttpStatus.OK);
  }

  @ApiOperation("API-条件分页查询")
  @Log(title = "条件分页查询", businessType = BusinessType.GET)
  @PreAuthorize("hasAuthority('/api/system/system-app/list/query')")
  @GetMapping("list/query")
  public Result findListPage (SystemAppQueryVo vo) {
    // 创建配置对象
    int page = 1;
    int pageSize = 20;
    if (vo.getPage() != null) {
      page = vo.getPage();
    }
    if (vo.getPageSize() != null) {
      pageSize = vo.getPageSize();
    }
    Page<SystemApp> pagination = new Page<>(page, pageSize);
    // 调用 Service 方法
    IPage<SystemApp> model = systemAppService.select(pagination, vo);
    return Result.ok(model, HttpStatus.OK);
  }

  @ApiOperation("API-添加一条数据")
  @Log(title = "添加一条数据", businessType = BusinessType.INSERT)
  @PreAuthorize("hasAuthority('/api/system/system-app/save')")
  @PostMapping("save")
  public Result save (@RequestBody SystemApp app) {
    boolean success = systemAppService.save(app);
    if (success) {
      return Result.ok(null, HttpStatus.OK);
    }
    return Result.fail();
  }

  @ApiOperation("API-修改数据")
  @Log(title = "修改数据", businessType = BusinessType.UPDATE)
  @PreAuthorize("hasAuthority('/api/system/system-app/update')")
  @PostMapping("update")
  public Result update (@RequestBody SystemApp app) {
    boolean success = systemAppService.updateById(app);
    if (success) {
      return Result.ok(null, HttpStatus.OK);
    }
    return Result.fail();
  }

  @ApiOperation("API-根据ID删除")
  @Log(title = "根据ID删除", businessType = BusinessType.DELETE)
  @PreAuthorize("hasAuthority('/api/system/system-app/delete')")
  @DeleteMapping("delete/{id}")
  public Result deleteById (@PathVariable String id) {
    boolean success = systemAppService.removeById(id);
    if (success) {
      return Result.ok(null, HttpStatus.OK);
    }
    return Result.fail();
  }

  @ApiOperation("API-根据多个ID批量删除")
  @Log(title = "根据多个ID批量删除", businessType = BusinessType.DELETE)
  @PreAuthorize("hasAuthority('/api/system/system-app/delete')")
  @DeleteMapping("delete")
  public Result deleteByIds (@RequestBody List<String> ids) {
    boolean success = systemAppService.removeByIds(ids);
    if (success) {
      return Result.ok(null, HttpStatus.OK);
    }
    return Result.fail();
  }
}
